package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.dto.resp.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.util.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;



    /**
     * 查询购物车
     * 请求url地址：/shoppingCart/list
     * 请求方式：GET
     * 请求参数：无
     * 响应结果【格式统一】：R<List<ShoppingCart>>
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //添加条件
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 添加购物车
     * 请求url地址：/shoppingCart/add
     * 请求方式：POST
     * 请求参数：JSON格式的数据，
     * 响应结果【格式统一】：R<ShoppingCart>
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        //设置当前用户的id
        shoppingCart.setUserId(userId);
        //获取当前购物车的套餐id
        Long setmealId = shoppingCart.getSetmealId();
        //判断下单的是套餐还是菜品
        //1.条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        if (setmealId != null) { //说明下单的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, setmealId);
        } else {//下单的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        //对cart进行判断
        if (cart != null) { //不是空的话,说明购物车中有该产品
            //获取该产品的数量
            Integer number = cart.getNumber();
            //将数量+1
            cart.setNumber(number + 1);
            shoppingCartService.updateById(cart);
        } else { //购物车中没有该产品 数量为1即可
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }
        return R.success(cart);
    }

    /**
     * 取消购物车
     * 请求url地址：/shoppingCart/sub
     * 请求方式：POST
     * 请求参数：JSON格式的数据，ShoppingCart接收
     * 响应结果【格式统一】：R<ShoppingCart>
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车信息:{}", shoppingCart);
        //获取当前登录用户的id
        Long userId = BaseContext.getCurrentId();
        //判断购物车中是菜品还是套餐
        //1.获取当前的菜品id
        Long dishId = shoppingCart.getDishId();
        //2.条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //3.判断购物车中是菜品还是套餐
        if (dishId != null) { //说明是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {//是菜品
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //获取当前对象
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        log.info("获取的对象cart:{}", cart);
        //获取当前的数量
        Integer number = cart.getNumber();
        log.info("number:{}", number);
        //对number判断
        if (number > 1) { //数量大于1时,数量-1
            cart.setNumber(number - 1);
            shoppingCartService.updateById(cart);
        } else {//数量<=0 直接删掉
            shoppingCartService.remove(wrapper);
        }
        return R.success(cart);
    }


    /**
     * 清空购物车
     * 请求url地址：/shoppingCart/clean
     * 请求方式：DELETE
     * 请求参数：无
     * 响应结果【格式统一】：R<String>
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //获取当前用户的id
        Long id = BaseContext.getCurrentId();
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //where user_id = id
        wrapper.eq(ShoppingCart::getUserId, id);
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }
}
